package com.zxw.auth.exception;

import com.zxw.common.core.base.enums.ResponseEnum;
import com.zxw.common.core.base.res.BaseRes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class AuthExceptionHandler {

    /**
     * 用户不存在
     *
     * @param e
     * @return
     */
    @ExceptionHandler(UsernameNotFoundException.class)
    public BaseRes handleUsernameNotFoundException(UsernameNotFoundException e) {
        return BaseRes.err(ResponseEnum.USERNAME_OR_PASSWORD_ERROR);
    }

    /**
     * 用户密码不正确
     * @param e
     * @return
     */
    @ExceptionHandler(InvalidGrantException.class)
    public BaseRes InvalidGrantException(InvalidGrantException e){
        return BaseRes.err(ResponseEnum.USERNAME_OR_PASSWORD_ERROR);
    }

    /**
     * 账户异常(禁用、锁定、过期)
     *
     * @param e
     * @return
     */
    @ExceptionHandler({InternalAuthenticationServiceException.class})
    public BaseRes handleInternalAuthenticationServiceException(InternalAuthenticationServiceException e) {
        return BaseRes.err(e.getMessage());
    }

    /**
     * token 无效或已过期
     *
     * @param e
     * @return
     */
    @ExceptionHandler({InvalidTokenException.class})
    public BaseRes handleInvalidTokenExceptionException(InvalidTokenException e) {
        return BaseRes.err(e.getMessage());
    }


}